package com.tc.travel.submitter.executor;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.tc.travel.submitter.pojo.VisaCancelDto;
import com.tc.travel.util.Consts;
import com.tc.travel.util.HttpClientUtil;
import com.tc.travel.util.PropertyUtil;
import com.tc.travel.util.SleepUtil;
import com.tc.travel.util.StringUtil;
import com.tc.travel.util.exception.ComRuntimeException;

@Component
public class VisaCancelExecutor extends BaseExecutor {
	private static Logger log = Logger.getLogger(VisaCancelExecutor.class);

	public void execute() {
		try {
			VisaCancelDto visaCancelDto = visaBo.getVisaForCancel();
			if (visaCancelDto != null) {
				String cookie = sessionBo.getCookie();
				log.info("cookie:" + cookie);

				int visaState = Consts.VISA_STATE_CANCEL_SUCC;
				String errorMsg = "";

				try {
					log.info("openSearchVisaPage...");
					openSearchVisaPage(cookie);

					log.info("getVisaIdentityId-->dest_visa_id:" + visaCancelDto.getDestVisaId() + "...");
					SleepUtil.sleepRandom(3, 6);
					String visaIdentityId = getVisaIdentityId(visaCancelDto.getDestVisaId(), cookie);
					log.info("visaIdentityId:" + visaIdentityId);

					log.info("cancelVisa...");
					SleepUtil.sleepRandom(5, 10);
					cancelVisa(visaIdentityId, cookie);

					// 更新状态为成功
					visaState = Consts.VISA_STATE_CANCEL_SUCC;
					errorMsg = "";
				} catch (Throwable e) {
					visaState = Consts.VISA_STATE_CANCEL_FAIL;
					errorMsg = e.getMessage();
					log.error("cancel visa error. visa_info_id=" + visaCancelDto.getVisaInfoId(), e);
				}

				log.info("updateVisaState:");
				visaBo.updateVisaState(visaCancelDto.getVisaInfoId(), visaCancelDto.getVisaCancelId(), visaState, visaCancelDto.getDestVisaId(), errorMsg);
			}
		} catch (Throwable e) {
			log.error("VisaCancelExecutor error: " + e.getMessage(), e);
		}
	}

	public void cancelVisa(String identityId, String cookie) {
		String url = PropertyUtil.destUrlVisaCancel;
		Map<String, String> params = new HashMap<String, String>();
		// agent info
		params.put("IDENTITY_ID", identityId);
		params.put("CANCEL_TYPE", "1");

		Map<String, String> headers = new HashMap<String, String>();
		headers.put("Cookie", cookie);
		headers.put("Accept", "application/json, text/javascript, */*; q=0.01");
		headers.put("Accept-Encoding", "gzip, deflate, br");
		headers.put("Accept-Language", "en,ja;q=0.9,en-US;q=0.8");
		headers.put("Connection", "keep-alive");
		headers.put("Content-Type", "application/x-www-form-urlencoded");
		headers.put("Host", "churenkyosystem.com");
		headers.put("Origin", "https://churenkyosystem.com");
		headers.put("Referer", "https://churenkyosystem.com/member/identity_list.php");
		headers.put("User-Agent", Consts.USER_AGENT);
		headers.put("X-Requested-With", "XMLHttpRequest");

		String jsonStr = HttpClientUtil.doPost(url, params, headers);
		assertCancelVisaReaultJson(jsonStr);
	}

	private void assertCancelVisaReaultJson(String jsonStr) {
		if (StringUtil.isEmpty(jsonStr)) {
			throw new ComRuntimeException("cancelVisa error; returned jsonStr is null.");
		}

		try {
			JsonObject jsonObj = new JsonParser().parse(jsonStr).getAsJsonObject();
			int status = jsonObj.get("status").getAsInt();
			if (status != 0) {
				throw new ComRuntimeException("cancelVisa error; return status is not zero: " + status);
			}
		} catch (ComRuntimeException e) {
			throw e;
		} catch (Exception e) {
			throw new ComRuntimeException("cancelVisa error; ", e);
		}
	}

	public static void main(String[] args) {
		new VisaCancelExecutor().assertCancelVisaReaultJson("{\"status\":0,\"message\":\"\u7121\u52b9\u51e6\u7406\u304c\u5b8c\u4e86\u3057\u307e\u3057\u305f\"}");
	}

}
